﻿
/*-------------------------------------
//
//Copyright (C) 2007 开发二组 版权所有。
//
//文件名：ResetPassword.ascx.cs
//
//
//文件功能描述：顾客修改密码....
//
//
//创建标识：2007.11.19 李正平
//创建描述：
//
// 
//修改：2007.11.28
//修改人：李正平
//修改：添加了顾客可以取消修改密码操作
//
//------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Security.Cryptography;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Common_Controls_resetPassword : System.Web.UI.UserControl
{
    //
    //新建一个实例,调用WebService操作
    //
    protected localhost.CustomerService customerResetPWD = new localhost.CustomerService();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["CustomerID"] != null)
            {

                //
                //获得当前顾客ID
                //
                string customerID = (string)Session["CustomerID"];                
            }
        }

    }

    /// <summary>
    /// 对数据进行加密
    /// </summary>
    /// <param name="strPassword"></param>
    /// <returns></returns>
    protected byte[] GetPassword(string strPassword)
    {

        //转化为二进制数组
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] arSource = UE.GetBytes(strPassword);

        //哈希加密
        SHA1 SHharsh = new SHA1Managed();
        byte[] arHarshValue = SHharsh.ComputeHash(arSource);

        //返回一个二进制数组
        return arHarshValue;
    }


    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        //
        //获得顾客登录密码
        //
        string customerID=(string)Session["CustomerID"];
        
        //
        //将密码加密
        //
        byte[] bytOldPWD=this.GetPassword(txtOldPwd.Text.Trim());
        
        //
        //将新密码加密
        //
        byte[] bytNewPWD=this.GetPassword(txtNewPwd.Text.Trim());        

        //
        //异步调用修改密码
        //
        customerResetPWD.UpdatePWDCompleted += new localhost.UpdatePWDCompletedEventHandler(customerResetPWD_UpdatePWDCompleted);
        customerResetPWD.UpdatePWDAsync(customerID, bytOldPWD, bytNewPWD);
    }



    /// <summary>
    /// 返回回调结果
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void customerResetPWD_UpdatePWDCompleted(object sender, localhost.UpdatePWDCompletedEventArgs e)
    {
        //
        //判断是否存在异步或是顾客取消操作
        //
        if (e.Error == null || e.Cancelled == false)
        {
            //
            //设置一个标志值，表示操作 是否成功
            //
            bool success = e.Result;
                   
            //判断修改是否成功
            if (success)
            {
                //
                //修改成功
                //
                this.lblMessage.Text = "恭喜您，密码修改成功！";
            }
            else
            {
                //
                //修改失败
                //
                lblMessage.Text = "抱歉，密码修改失败，请重新输入！";
            }
        

        }
        else
        {
            //
            //顾客取消修改操作或者在执行异步过程中出错
            //
            this.lblMessage.Text = "抱歉，密码修改失败，请重新输入！";
            throw e.Error;
        }
    }
   
}
